<?php
namespace app\controller;
use app\BaseController;
use app\model\Jinshu;
use app\model\Kll;
use app\model\Searchkll;
use think\facade\View;

class Mobile extends BaseController {
    //移动端万能add
    public function wnadd(){
        try {
            $post=$this->request->post();
            $post['foodname']=explode('(',$post['foodname'])[0];
            $skllm=new Searchkll();
            if($post['foodname']==''&&$post['zhongliang']==100){
                $skllm->name=$post['searchname'];
                $skllm->reliang=$post['reliang'];
                $skllm->time=time();
                $skllm->save();
            }
            if(isset($post['foodname'])){
                $reliang=$skllm->where('name',$post['foodname'])
                    ->find();
                $kllm=new Kll();
                $kllm->type=3;
                $kllm->kll=$post['zhongliang']*$reliang['reliang']/100;
                $kllm->name=$post['foodname'];
                $kllm->zl=$post['zhongliang'];
                $kllm->time=gettime();
                $kllm->save();
                $reliang=$skllm->where('name',$post['foodname']);
                $reliang->save(['time'=>time()]);
            }
        }catch (\Exception $e){
            return redirect('/mobile');

        }
        return redirect('/mobile');
    }
    //根据现有食物和重量算出总热量
    public function srl($searchkey='',$foodname='',$zhongliang=0){
        $skllm=new Searchkll();
        $foodname=explode('(',$foodname)[0];
        $find=$skllm->where(['name'=>$foodname])
            ->find();
        if($find){
            return $find->reliang*$zhongliang/100;
        }
    }
    public function index(){
        //这个if是用来录入新食物信息的
        if($post=$this->request->post()){
            $modelkll=new Kll();
            $modelkll->type=$post['type'];
            $modelkll->kll=$post['reliang']*$post['zl']/100;
            $modelkll->name=$post['name'];
            $modelkll->zl=$post['zl'];
            $modelkll->time=gettime();
            $modelkll->save();
            $modelsw=new Searchkll();
            $modelsw->where('name',$post['name'])
                ->update(['time'=>time()]);
        }
        $kllm=new Kll();
        $kll8=$kllm->where('time',gettime())
            ->where('type',3)
            ->order('id desc')
            ->select()
            ->toArray();
        View::assign('kll8',$kll8);
        $model=new Searchkll();
        $arr=$model->order('time desc')
            ->limit(50)
            ->select()
            ->toArray();
        foreach ($arr as $key=>$v){
            if($v['reliang']<=100){
                $arr[$key]['dengji']='list-group-item-success';
                $arr[$key]['name2']=$v['name'].'(低)'.'  '.' '.$v['reliang'].' kcal/100g';
            }
            if($v['reliang']>100&&$v['reliang']<=150){
                $arr[$key]['dengji']='list-group-item-primary';
                $arr[$key]['name2']=$v['name'].'(中)'.'  '.' '.$v['reliang'].' kcal/100g';
            }
            if($v['reliang']>150&&$v['reliang']<=200){
                $arr[$key]['dengji']='list-group-item-danger';
                $arr[$key]['name2']=$v['name'].'(高)'.'  '.' '.$v['reliang'].' kcal/100g';
            }
            if($v['reliang']>200){
                $arr[$key]['dengji']='list-group-item-dark';
                $arr[$key]['name2']=$v['name'].'(极高)'.'  '.' '.$v['reliang'].' kcal/100g';
            }
        }
        View::assign('arr',$arr);
        //输出今日体重
        $mjinshu=new Jinshu();
        $find=$mjinshu->where('time',gettime())
            ->find();
        if($find){
            $tizhong=$find->jinshu;
        }else{
            $tizhong='今日体重未录入';
        }
        View::assign('tz',$tizhong);
        //录入今日运动消耗
        $mjinshu=new Kll();
        $find=$mjinshu->where('time',gettime())
            ->where('type',2)
            ->find();
        if($find){
            $tizhong=$find->kll;
        }else{
            $tizhong='今日运动未录入';
        }
        View::assign('kll',$tizhong);
        /*减肥目标和今日差距
        1kg热量值是7500大卡
        */
        $kg=(220-160)/2;
        $zhongyao['kg']=$kg;
        //总消耗千卡
        $zkll=$kg*7500;
        $zhongyao['zkll']=$zkll;
        //平均每天消耗卡路里
        $daykll=$zkll/180;
        $zhongyao['daykll']=$daykll;
        //今日基础消耗，取最近一天的值
        $model=new Kll();
        //基础消耗
        $daybase=$model->order('time desc')->where('time',gettime())->where(['type'=>1])->value('kll');
        $zhongyao['daybase']=$daybase;
        //运动消耗
        $dayxiaohao=$model->order('time desc')->where(['type'=>2,'time'=>gettime()])->value('kll');
        $zhongyao['dayxiaohao']=$dayxiaohao;
        //实际摄入
        $dayeat=$model->order('time desc')->where(['type'=>3,'time'=>gettime()])->sum('kll');
        $zhongyao['dayeat']=$dayeat;
        //实际消耗字符串
        $xiaohaokll='基础消耗（'.$daybase.')+运动消耗('.$dayxiaohao.')-摄入热量('.$dayeat.')='.($daybase+$dayxiaohao-$dayeat).'千卡';
        $zhongyao['xiaohaokll']=$xiaohaokll;
        //实际消耗
        $sjxh=$daybase+$dayxiaohao-$dayeat;
        $zhongyao['sjxh']=$sjxh;
        //每天减重
        $dayjz=$daykll/7500*2;
        $zhongyao['dayjz']=$dayjz;
        //今日完成比例
        $wanchengbl=($daybase+$dayxiaohao-$dayeat)/$daykll*100;
        $zhongyao['wanchengbl']=$wanchengbl;
        //今日摄入食物
        $eatlist=$model->where(['type'=>3,'time'=>gettime()])->order('kll desc')
            ->select()->toArray();
        foreach ($eatlist as $k=>$v){
            $eatlist[$k]['hebing']=$v['name'].$v['zl'].'克'.$v['kll'].'大卡';
        }
        $zhongyao['eatlist']=$eatlist;
        //相当于慢跑多长时间

        View::assign('zhongyao',$zhongyao);
        return View::fetch();
    }
}
